﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace htmltortf
{
    public partial class Slide : Form
    {
        public int livroid, top;
        public decimal capitulo, versiculomax, versiculo, versiculoinicial;
        public FormState fs = new FormState();
        public Slide()
        {
            InitializeComponent();
        }
        public void Carregar(int plivroid, decimal pcapitulo, decimal pversiculomax, decimal pversiculo)
        {
            livroid = plivroid;
            capitulo = pcapitulo;
            versiculomax = pversiculomax;
            versiculo = pversiculo;
            versiculoinicial = pversiculo;
        }
        private void Slide_Load(object sender, EventArgs e)
        {
            LivroTableAdapters.LivrosTableAdapter lta = new LivroTableAdapters.LivrosTableAdapter();
            Livro.LivrosDataTable ldt = lta.GetDataLivroid(livroid);
            this.Text = ldt.Rows[0]["livro"].ToString() + " - Capitulo " + capitulo + " - "+versiculo+" : "+versiculomax;
            SlideLoad();
        }

        private void SlideLoad()
        {
            rtfSlide.MaximumSize = new Size(this.Width, this.Height);
            rtfSlide.SelectionIndent = 5;
            rtfSlide.SelectionRightIndent = 5;
            
            ConteudoTableAdapters.ConteudoTableAdapter cta = new ConteudoTableAdapters.ConteudoTableAdapter();
            Conteudo.ConteudoDataTable cdt;
            //if (versiculo > 0)
            //{
                cdt = cta.GetDataByLivroCapitulo(livroid, Convert.ToInt32(capitulo), Convert.ToInt32(versiculo), Convert.ToInt32(versiculo));
            //}
            //else
            //{
            //    cdt = cta.GetDataByLivroCapitulo(livroid, Convert.ToInt32(capitulo), Convert.ToInt32(versiculoinicial), Convert.ToInt32(versiculomax));
            //}
            for (int i = 0; i < cdt.Rows.Count; i++)
            {
                Conteudo.ConteudoRow cr = (Conteudo.ConteudoRow)cdt.Rows[i];
                rtfSlide.Text = cr.versiculo + " - " + cr.palavra;
            }
            //rtfSlide.Top = Convert.ToInt32(this.Width / 8);
            //rtfSlide.Width = this.Width - 10;
            //rtfSlide.Height = this.Height - rtfSlide.Top;


            ConfiguracaoTableAdapters.ConfiguracaoTableAdapter confta = new ConfiguracaoTableAdapters.ConfiguracaoTableAdapter();
            Configuracao.ConfiguracaoDataTable confdt = confta.GetDataById(1);
            Configuracao.ConfiguracaoRow confr = (Configuracao.ConfiguracaoRow)confdt.Rows[0];
            FontStyle style = new FontStyle();//rtbConteudo.SelectionFont.Style;
            if (confr.bold)
            {
                style |= FontStyle.Bold;
            }
            if (confr.italic)
            { style |= FontStyle.Italic; }
            if (confr.strikeout) { style |= FontStyle.Strikeout; }
            if (confr.underline) { style |= FontStyle.Underline; }
            try
            {
                Font f = new Font(confr.font, (float)confr.fontsize, style);

                rtfSlide.Font = f;
            }
            catch { }
            this.BackColor = System.Drawing.ColorTranslator.FromHtml(confr.backcolor);
            rtfSlide.BackColor = System.Drawing.ColorTranslator.FromHtml(confr.backcolor);
            rtfSlide.ForeColor = System.Drawing.ColorTranslator.FromHtml(confr.fontcolor);
            //rtfSlide.SelectAll();
            //rtfSlide.SelectionCharOffset = 100;
            rtfSlide.SelectionAlignment = HorizontalAlignment.Center;
            //Padding p = new Padding();
            //p.Top = Convert.ToInt32(rtfSlide.Height/2);
            //rtfSlide.Margin = p;

            Graphics x = rtfSlide.CreateGraphics();
            SizeF s = x.MeasureString(rtfSlide.Text, rtfSlide.Font);
            double widthpreferencial = s.Width + rtfSlide.SelectionRightIndent + rtfSlide.SelectionIndent;
            rtfSlide.Width = Convert.ToInt32(widthpreferencial);
            int linhas = Convert.ToInt32(Math.Truncate(widthpreferencial / rtfSlide.Width)) + 1;
            rtfSlide.Height = Convert.ToInt32(s.Height * linhas) + 1 + rtfSlide.Margin.Top + rtfSlide.Margin.Bottom + Convert.ToInt32(s.Height / 2);
            //if (Scrool.vsscrool(rtfSlide)) { rtfSlide.Height += Convert.ToInt32(s.Height); }
            rtfSlide.Left = (this.ClientSize.Width - rtfSlide.Width) / 2;
            rtfSlide.Top = (this.ClientSize.Height - rtfSlide.Height) / 2;
            top = rtfSlide.Top;
        }

        private void Slide_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right)
            {
                if (versiculo < versiculomax)
                {
                    versiculo += 1;
                    SlideLoad();
                }
            }
            else if (e.KeyCode == Keys.Left)
            {
                if (versiculo > 1)
                {
                    versiculo -= 1;
                    SlideLoad();
                }
            }
        }

        private void rtfSlide_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right)
            {
                if (versiculo < versiculomax)
                {
                    versiculo += 1;
                    SlideLoad();
                }
            }
            else if (e.KeyCode == Keys.Left)
            {
                if (versiculo > 1)
                {
                    versiculo -= 1;
                    SlideLoad();
                }
            }
            else if (e.KeyCode == Keys.F11)
            {
                fs.Maximize(this);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                fs.Restore(this);
            }
            e.Handled = true;
        }

        private void Slide_ResizeEnd(object sender, EventArgs e)
        {
            SlideLoad();
        }

        private void Slide_ClientSizeChanged(object sender, EventArgs e)
        {
            SlideLoad();
        }
    }
}
